package com.hqyj.hrms.modules.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.hrms.modules.account.dao.PowerMapper;
import com.hqyj.hrms.modules.account.dao.RoleMapper;
import com.hqyj.hrms.modules.account.entity.Power;
import com.hqyj.hrms.modules.account.entity.Role;
import com.hqyj.hrms.modules.account.service.PowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class PowerServiceImpl implements PowerService {

    //注入mapper
    @Autowired(required = false)
    PowerMapper powerMapper;

    @Autowired(required = false)
    PowerService powerService;

    @Override
    public HashMap<String, Object> add(Power power) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = powerMapper.insert(power);
        if (num > 0) {
            map.put("info", "新增成功");
        } else {
            map.put("info", "新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Power power) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = powerMapper.updateById(power);
        if (num > 0) {
            map.put("info", "修改成功");
        } else {
            map.put("info", "修改失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = powerMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> list(Power power) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //第一步：创建分页对象
        Page<Power> powerInfoPage = new Page<>(power.getPage(), power.getRow());
        //创建查询条件对象
        QueryWrapper<Power> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(
                power.getPowerName() != null && !power.getPowerName().equals(""),
                "role_name",
                power.getPowerName());

        //第二步：查询分页，获取分页数据集
        Page<Power> pageList = powerMapper.selectPage(powerInfoPage, queryWrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

   /* @Override
    public HashMap<String, Object> selectPower(String userName) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Power> list = (List<Power>) powerService.selectPower(userName);
        List<Power> list1 = new ArrayList<>();
        if (list != null) {
            //构建权限菜单的数据结构
            for (Power p1 : list) {
                if (p1.getParentId() == 0) {
                    Power p2 = new Power();
                    p2.setPowerUrl(p1.getPowerUrl());
                    p2.setPowerUrl(p1.getPowerIndex());
                    p2.setPowerUrl(p1.getPowerName());
                    //创建一个集合
                    List<Power> listChilder = new ArrayList<>();
                    //查询子菜单
                    for (Power m : list) {
                        if (m.getParentId() == p1.getNodeId()) {
                            listChilder.add(m);
                        }
                    }
                    p2.setChildren(listChilder);
                    list1.add(p2);
                }
            }
        }
        map.put("info", list1);
        return map;
    }*/
}
